﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using ZhuXingFamilys.ExportExcel.entity;

namespace ZhuXingFamilys.ExportExcel.utils
{
    /// <summary>
    /// 生成专业树工具
    /// </summary>
    public static class CategoryTree
    {
        public static ObservableCollection<DefinitionNode> getCategoryTree(ObservableCollection<excelData> excelDataList)
        {
            if (excelDataList != null && excelDataList.Count != 0)
            {
                ObservableCollection<DefinitionNode> list = new ObservableCollection<DefinitionNode>();
                Dictionary<String, String> map = getCategory();
                foreach (var item in map)
                {
                    // 类型
                    ObservableCollection<DefinitionNode> excelDataName = getExcelDataName(item.Key, excelDataList);
                    // 楼层
                    ObservableCollection<DefinitionNode> excelDataElevation = getExcelDataElevation(item.Key, excelDataList);
                    if (!item.Key.Equals("0"))
                    {
                        list.Add(new DefinitionNode()
                        {
                            Name = item.Value,
                            SpecialitieId = item.Key,
                            Children = new ObservableCollection<DefinitionNode>()
                        {
                        new DefinitionNode()
                        {
                            Name = "类型",
                            SpecialitieId = item.Key,
                            Parent = item.Value,
                            Children = excelDataName
                        },
                        new DefinitionNode()
                        {
                            Name = "楼层",
                            SpecialitieId = item.Key,
                            Parent = item.Value,
                            Children = excelDataElevation
                        }
                            }
                        });
                    }
                    else
                    {
                        list.Add(new DefinitionNode()
                        {
                            Name = item.Value,
                            SpecialitieId = item.Key
                        });
                    }
                }
                return list;
            }
            else
            {
                ObservableCollection<DefinitionNode> listNull = new ObservableCollection<DefinitionNode>();
                Dictionary<String, String> map = getCategory();
                foreach (var item in map)
                {
                    if (!item.Key.Equals("0"))
                    {
                        listNull.Add(new DefinitionNode()
                        {
                            Name = item.Value,
                            SpecialitieId = item.Key,
                            Children = new ObservableCollection<DefinitionNode>()
                        {
                        new DefinitionNode()
                        {
                            Name = "类型",
                            SpecialitieId = item.Key,
                            Parent = item.Value
                        },
                        new DefinitionNode()
                        {
                            Name = "楼层",
                            SpecialitieId = item.Key,
                            Parent = item.Value
                        }
                            }
                        });
                    }
                    else
                    {
                        listNull.Add(new DefinitionNode()
                        {
                            Name = item.Value,
                            SpecialitieId = item.Key
                        });
                    }
                }
                return listNull;
            }
        }

        /**
         * 查询分类
         */
        private static Dictionary<string, string> getCategory()
        {
            Dictionary<string, string> map = new Dictionary<string, string>();
            map.Add("0", "全部");
            map.Add("1", "建筑");
            map.Add("2", "装修");
            map.Add("3", "安装");
            map.Add("4", "市政");
            map.Add("5", "施工");
            return map;
        }
        /**
         * 查询类型
         */
        private static ObservableCollection<DefinitionNode> getExcelDataName(string value, ObservableCollection<excelData> excelDataList)
        {
            ObservableCollection<DefinitionNode> list = new ObservableCollection<DefinitionNode>();
            ObservableCollection<DefinitionNode> tempList = new ObservableCollection<DefinitionNode>();
            foreach (excelData excel in excelDataList)
            {
                if (excel.SpecialitieId == int.Parse(value))
                {
                    //string name = excel.Name;
                    string name = excel.Category;
                    list.Add(new DefinitionNode()
                    {
                        Name = name,
                        SpecialitieId = value,
                        Parent = "类型"

                    });
                }
            }
            foreach (DefinitionNode node in list)
            {
                if (!tempList.Contains(node))
                {
                    tempList.Add(node);
                }
            }
            return tempList;
        }
        /**
         * 查询楼层
         */
        private static ObservableCollection<DefinitionNode> getExcelDataElevation(string value, ObservableCollection<excelData> excelDataList)
        {
            ObservableCollection<DefinitionNode> list = new ObservableCollection<DefinitionNode>();
            ObservableCollection<DefinitionNode> tempList = new ObservableCollection<DefinitionNode>();
            if (!("0").Equals(value))
            {
                foreach (excelData excel in excelDataList)
                {
                    if (excel.SpecialitieId == int.Parse(value))
                    {
                        string elevation = excel.Elevation;
                        //string floor = excel.Floor;
                        list.Add(new DefinitionNode()
                        {
                            Name = elevation,
                            SpecialitieId = value,
                            Parent = "楼层"

                        });
                    }
                }
                foreach (DefinitionNode node in list)
                {
                    if (!tempList.Contains(node) && !string.IsNullOrEmpty(node.Name)) //去除空楼层
                    {
                        tempList.Add(node);
                    }
                }
            }
            // 排序
            for (int i = 0; i < tempList.Count - 1; i++)
            {
                for (int j = 0; j < tempList.Count - i - 1; j++)
                {
                    int int1 = getNumber(tempList[j].Name);
                    int int2 = getNumber(tempList[j + 1].Name);
                    if (int1 > int2)
                    {
                        var temp = tempList[j];
                        tempList[j] = tempList[j + 1];
                        tempList[j + 1] = temp;
                    }
                }
            }
            return tempList;
        }
        // 提取数字
        private static int getNumber(string str)
        {
            int number = 0;
            string num = null;
            foreach (char item in str)
            {
                if (item >= 48 && item <= 58)
                {
                    num += item;
                }
            }
            if (num != null)
            {
                number = int.Parse(num);
                return number;
            }
            return 99999;
        }
    }
}
